import java.awt.BorderLayout;
import java.awt.FlowLayout;

import javax.swing.*;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.util.*;
//Current Status - The manufacturer method is working. 
//TO DO - Make the other methods return an arraylist for the drop down menu to keep updating.
//TO DO - Make the vendor to be working to.
// TO DO - Insertion,deletion, Update - Add a text area at LAyout.SOUTH

public class QueryVendorPartGUI extends JFrame 
{
        ArrayList<String> values = new ArrayList<String>();// The selected values.

        private String selected = "";

        private JTextField displayField = new JTextField(30);

        JFrame frame = new JFrame();

        private JComboBox c = new JComboBox();

        private JButton b = new JButton("Add item");
        

        private int count = 0, p = 0;;
        String manuf ="" ;
        String model = "", year= "", description = "",litres, inches= "", engine= "";

        ArrayList<String> temp;
        //ArrayList<String> temp2;

        public QueryVendorPartGUI(Connection connection, ConnectDb connectDb) 
        {
                //-------------------------------------------------------------------
                final queryVendorPart q = new queryVendorPart(connection, connectDb);

                final JPanel displayPanel = new JPanel() ; // the upper panel.
                final JPanel optionPanel = new JPanel() ; // the 2nd panel which displays the content.
                

                        temp  = q.queryMaker();
                        int x = temp.size();
                        p++;
                        for(int i = 0;i<x;i++)
                                c.addItem(temp.get(i));
                        //----------------------------------------
                        displayField.setEditable(false);
                        b.addActionListener(new ActionListener() {
                                public void actionPerformed(ActionEvent e) 
                                {                       
                                    
                                    selected = (String) c.getSelectedItem();
                                    System.out.println(selected);
                                    if(selected == null)
                                    {
                                        selected = "";
                                    }
                                    
                                    if(count == 0)
                                    {
                                        //q.queryTName(selected.trim());
                                        //count++;
                                        //return;
                                        q.queryTName(selected.trim());
                                        temp = q.queryPnum();
                                        
                                    }

                                    else if(count == 1)
                                    {
                                        //displayPanel.removeAll();
                                        //optionPanel.removeAll();
                                        //frame.removeAll();
                                        //frame.repaint();
                                        displayPanel.setVisible(false);
                                        optionPanel.setVisible(false);
                                        JPanel panel = new JPanel();
                                        
                                        ArrayList<String> parts = q.queryParts(temp.get(0));
                                        printList(parts);

                                        JList jlist = new JList(parts.toArray());
                                        JScrollPane pane = new JScrollPane(jlist);

                                        panel.add(pane);
                                        panel.setVisible(true);
                                        frame.add(panel);
                                        frame.repaint();
                                    }
                                    
                                    c.removeAllItems();
                                    if (temp == null || temp.isEmpty() || temp.equals(null))
                                        frame.setVisible(false);
                                    else
                                    for(int i = 0; i < temp.size(); i++)
                                    {
                                        c.addItem(temp.get(i));
                                    }

                                    count++;
                                }
                        });





                optionPanel.setLayout(new FlowLayout());
                displayPanel.add(displayField);
                optionPanel.add(c);
                optionPanel.add(b);
                //connectDb.disconnectFromDB();

                frame.setLayout(new BorderLayout());
                frame.add(displayPanel,BorderLayout.NORTH);
                frame.add(optionPanel,BorderLayout.CENTER);
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setSize(400, 400);
                frame.setVisible(true);

        }
        
        public void printList(ArrayList<String> a)
        {
            for(int i = 0; i < a.size(); i++)
                System.out.println("LIST:" + a.get(i));
        }
        


}

